package ext.generic.project.reports;

import java.lang.reflect.InvocationTargetException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import wt.inf.team.ContainerTeam;
import wt.inf.team.ContainerTeamHelper;
import wt.inf.team.ContainerTeamManaged;
import wt.method.RemoteAccess;
import wt.method.RemoteMethodServer;
import wt.org.WTGroup;
import wt.org.WTPrincipal;
import wt.org.WTPrincipalReference;
import wt.org.WTUser;
import wt.project.Role;
import wt.projmgmt.admin.Project2;
import wt.util.WTException;
import wt.util.WTProperties;
import ext.generic.project.reports.bean.ProjectTeamReportBean;

public class ProjectTeamCommand implements RemoteAccess{
	private static boolean VERBOSE;
	private static String codebalocation;
	private static String CLASSNAME = ProjectTeamCommand.class.getName();
	private static WTProperties wtProperties = null;
	static {
		try {
			wtProperties = WTProperties.getLocalProperties();
			VERBOSE = (WTProperties.getLocalProperties()).getProperty("ext.generic.project.reports.verbose", false);
			codebalocation = wtProperties.getProperty("wt.codebase.location", "");
		} catch (Throwable throwable) {
			throw new ExceptionInInitializerError(throwable);
		}
	}
	
	

	public ProjectTeamCommand() {
		
	}

	public static ProjectTeamCommand newProjectTeamCommand(){
		
		return new ProjectTeamCommand();
	}
	
	/**
	 * 
	 * @param nmcommandbean
	 * @param s
	 * @return
	 * @throws WTException
	 */
	public static List findSharedTeamMembers(Project2 project) throws WTException {
		if(VERBOSE){
			System.out.println(CLASSNAME+".findSharedTeamMembers("+project+")");
		}
		ArrayList arraylist = new ArrayList();
		if(project==null){
			return arraylist;
		}
		if (!RemoteMethodServer.ServerFlag) {
			System.out.println("远程调用findSharedTeamMembers");
			try {
				arraylist=(ArrayList) RemoteMethodServer.getDefault().invoke("findSharedTeamMembers",CLASSNAME,null,new Class[] {Project2.class}, new Object[] {project});
			} catch (RemoteException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
			return 	arraylist;
			
		}
		 if(project instanceof ContainerTeamManaged)
	        {
		        ContainerTeam containerTeam = ContainerTeamHelper.service.getContainerTeam((ContainerTeamManaged)project);
		        Vector roles =containerTeam.getRoles();
		        for(int z=0;z<roles.size();z++){
		        	ArrayList participants=new ArrayList();
		        	Role role=(Role) roles.get(z);
		        	participants =containerTeam.getAllPrincipalsForTarget(role);
		        	for(int p=0;p<participants.size();p++){
		        		WTPrincipal wtpr=(WTPrincipal) ((WTPrincipalReference)participants.get(p)).getObject();
		        		if(wtpr instanceof WTUser){
		        			WTUser user=(WTUser) wtpr;
		        			if(user!=null){
		        				ProjectTeamReportBean bean=new ProjectTeamReportBean();
		        				bean.setRoleName(role.getDisplay());
		        				bean.setUserName_GroupName(user.getFullName()+":"+user.getName());
		        				if(VERBOSE){
		        					System.out.println("find role "+role.getDisplay()+" Member:"+user.getFullName()+":"+user.getName());
		        				}
		        				arraylist.add(bean);
		        			}
		        		}else if(wtpr instanceof WTGroup){
		        			WTGroup group=(WTGroup) wtpr;
		        			if(group!=null){
		        				ProjectTeamReportBean bean=new ProjectTeamReportBean();
		        				bean.setRoleName(role.getDisplay());
		        				bean.setUserName_GroupName(group.getName());
		        				if(VERBOSE){
		        					System.out.println("find role "+role.getDisplay()+" Member:"+group.getName());
		        				}
		        				arraylist.add(bean);
		        			}
		        		}
		        	}
		        }
	        }else{
	        	if(VERBOSE){
	        		System.out.println("******************************************");
	        	}
	        }
		return arraylist;
		
	}

	
}
